package com.tocean.app.controller.common;

import com.tocean.app.controller.request.datadict.DataDictOperateDto;
import com.tocean.app.controller.request.datadict.DataDictSearchDto;
import com.tocean.app.dto.request.BasePageRequest;
import com.tocean.app.dto.request.BaseRequest;
import com.tocean.app.dto.response.BasePageResponse;
import com.tocean.app.dto.response.BaseResponse;
import com.tocean.app.entity.common.DataDict;
import com.tocean.app.service.common.DataDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "数据字典")
@Slf4j
@RestController
@RequestMapping("/common/data-dict")
public class DataDictController {

    @Autowired
    private DataDictService dataDictService;

    @ApiOperation(value = "查询数据字典")
    @PostMapping("/page")
    public BaseResponse<BasePageResponse<DataDict>> page(@Valid @RequestBody BasePageRequest<DataDictSearchDto> request) {
        return BaseResponse.success(dataDictService.page(request.getData(), request.getPage(), request.getLimit()));
    }

    @ApiOperation(value = "根据[字典标识]查询所以下级")
    @GetMapping("/{dataDictId}/children")
    public BaseResponse<List<DataDict>> findDataDictByParentId(@PathVariable("dataDictId") String dataDictId) {
        return BaseResponse.success(dataDictService.findDataDictById(dataDictId));
    }

    @ApiOperation(value = "根据[字典唯一标识]查询所以下级")
    @GetMapping("/dict-key/{dictKey}")
    public BaseResponse<DataDict> findDataDictByDictKey(@PathVariable("dictKey") String dictKey) {
        return BaseResponse.success(dataDictService.findDataDictByDictKey(dictKey));
    }

    @ApiOperation(value = "新增/更新词典")
    @PostMapping
    public BaseResponse<?> saveOrUpdate(@Valid @RequestBody BaseRequest<DataDictOperateDto> operateDto) {
        dataDictService.saveOrUpdate(operateDto.getData());
        return BaseResponse.success();
    }

    @ApiOperation(value = "根据删除词典")
    @DeleteMapping("/{dataDictId}")
    public BaseResponse<?> delete(@PathVariable("dataDictId") String dataDictId) {
        dataDictService.delete(dataDictId);
        return BaseResponse.success();
    }

}
